package com.kehutong.payfees.admin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.kehutong.payfees.util.ExcelUtil;
import com.kehutong.payfees.util.JDBCUtil;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;

import com.kehutong.common.ExcelHeaders;
import com.kehutong.common.Java8TimeUtils;
import com.kehutong.common.math.CalculatorUtils;
import com.kehutong.payfees.entity.record.ChargeRecord;
import com.kehutong.payfees.enums.WeChatBankTypeEnum;
import com.kehutong.payfees.service.ChargeBillServiceImpl;
import org.coraframework.orm.jdbc.execute.entity.FindArray;
import org.coraframework.orm.jdbc.execute.entity.FindPage;

/**
 * 资金流水
 */
@MvcAction
@ReqMapping("/admin/charge/record")
public class ChargeRecordAction {
    @Inject
    private ChargeBillServiceImpl chargeBillService;
    @Inject
    private JdbcSession jdbcSession;

    /**
     * 资金流水分页列表
     */
    @HasPermission("charge:record:view")
    @ReqMapping("/page")
    public Object page(JSONObject req) {
        FindPage<ChargeRecord> find = jdbcSession.findPage(ChargeRecord.class)
                .eq(req,  "bizType")
                .like(req, "transactionId", "mbrName", "mobile")
                .eq("result", true)
                .order("chargeTime", "DESC")
                .page(req);

        JDBCUtil.between(req, find, "chargeTime");

        Page<ChargeRecord> data = find.exe();

        return Result.success(data);
    }

    /**
     * 资金流水列表导出
     */
    @HasPermission("charge:record:view")
    @ReqMapping("/export")
    public Object export(JSONObject req) throws IOException {
        FindArray<ChargeRecord> find = jdbcSession.findArray(ChargeRecord.class)
                .eq(req, "bizType")
                .like(req, "transactionId", "mbrName", "mobile")
                .eq("result", true)
                .order("chargeTime", "DESC");

        JDBCUtil.between(req, find, "chargeTime");

        List<ChargeRecord> crs = find.exe();

        List<String[]> objList = new ArrayList<>(crs.size());
        for (int i = 0;i < crs.size();i++) {
            ChargeRecord record = crs.get(i);

        	String[] rStr = new String[9];
            rStr[0] = String.valueOf(i+1);
        	rStr[1] = record.getExChangeCode();
        	rStr[2] = record.getTransactionId();
        	rStr[3] = record.getMchName();

        	rStr[4] = record.getMbrName();
            rStr[5] = record.getMobile();
        	rStr[6] = CalculatorUtils.formatMoneyString(record.getAmt());
        	rStr[7] = Java8TimeUtils.parseString(record.getChargeTime());
        	rStr[8] = record.getBizType().getDesc();
        	objList.add(rStr);
        }
        
        File tmpFile = ExcelUtil.writeXlsFile("资金流水列表导出.xls", ExcelHeaders.CHARGE_STATISTICS_CHARGE_RECORD_EXPORT_EXCEL_HEADER, objList);
        return Result.download(tmpFile);
    }

    /**
     * 资金流水详情
     */
    @HasPermission("charge:record:view")
    @ReqMapping("/get")
    public Object get(String id) {
        ChargeRecord cr = Pool.get(ChargeRecord.class, id);
        if (cr == null) {
            return Result.error(0, "不存在的账单");
        }

        JSONObject v = new JSONObject();
        v.put("id", cr.getId());

        v.put("mchId", cr.getMchId());
        v.put("mchName", cr.getMchName());
        v.put("exChangeCode", cr.getExChangeCode());
        v.put("subMchId", cr.getSubMchId());
        v.put("submchName", cr.getSubmchName());
        v.put("amt", cr.getAmt());
        v.put("chargeTime", cr.getChargeTime());
        v.put("bizType", cr.getBizType());
        v.put("transactionId", cr.getTransactionId());
        v.put("mobile", cr.getMobile());
        v.put("mbrName", cr.getMbrName());

        v.put("bankType", WeChatBankTypeEnum.parseName(cr.getBankType()));

        return Result.success(v);
    }

}
